// pages/advisoryDetail/davisoryDetail.js
var that;
Page({

  data: {
    petType: [
      '猫猫', "狗狗"
    ],
    pet: [{
      "type": "", "judge": "", "judge2": "","judge3":""
    }],
    time: '',
    multiArray: ['小于1天', '大于1天小于1周', '大于1周小于1月', '大于1月'],
    multiIndex: [0],
    image: [],
    img: getApp().globalData.urls + "/images/consult_propaganda.png",
    rotate:'',
  },
  onLoad: function (options) {
    that = this;
    that.setData({
      show: false,
      opacity: 0
      //  date:setTime()
    })
  },


  radioChangePet(e) {
    let list = that.data.pet;
    list[0].type = e.detail.value
    that.setData({
      pet: list
    })
  },
  radioChangeJudge(e) {
    let list = that.data.pet;
    list[0].judge = e.detail.value
    that.setData({
      pet: list
    })
  },
  radioChangeJudge2(e) {
    let list = that.data.pet;
    list[0].judge2 = e.detail.value
    that.setData({
      pet: list
    })
  },
  radioChangeJudge3(e) {
    let list = that.data.pet;
    list[0].judge3 = e.detail.value
    that.setData({
      pet: list
    })
  },
  slePic(e) {
    var PIC = that.data.image;
    wx.chooseImage({
      count: 9 - that.data.image.length,
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        PIC = PIC.concat(tempFilePaths)
        that.setData({
          image: PIC
        })
        console.log(that.data.image)
      },
    })
  },
  PreviewImage(e) {
    var ind = e.currentTarget.dataset.ind
    wx.previewImage({
      current: that.data.image[ind], // 当前显示图片的http链接
      urls: that.data.image // 需要预览的图片http链接列表
    })
  },
  delectPic(e) {
    var PIC = that.data.image;
    PIC.splice(e.currentTarget.dataset.src, 1);
    that.setData({
      image: PIC
    })
  },
  // bindTimeChange(e){
  //   console.log(e)
  // },
  // bindcolumnTimechange(e) {
  //   console.log(e)
  // },
  bindMultiPickerChange: function (e) {
    var res = e.detail.value
    console.log(res)
    that.setData({
      time: that.data.multiArray[res],
      multiIndex:res ,
      rotate:0

    })
  },
  rotate:function(){
    that.setData({
      rotate:180

    })
  },
  rotateCancel:function(){
    that.setData({
      rotate: 0

    })
  },
  formSubmit: function (e) {
    var value = e.detail.value
    var formId = e.detail.formId
    console.log(e)
    if (value.type == '') {
      wx.showToast({
        title: '请选择宠物种类'
      })
    } else if (value.sex == '') {
      wx.showToast({
        title: '请输入宠物性别'
      })
    } else if (value.petName == '') {
      wx.showToast({
        title: '请输入宠物名称'
      })
    } else if (value.petAge == '') {
      wx.showToast({
        title: '请输入宠物年龄'
      })
    } else if (value.ster == '') {
      wx.showToast({
        title: '请选择是否绝育'
      })
    } else if (that.data.time == '') {
      wx.showToast({
        title: '请选择发病时长'
      })
    } else if (value.treat == '') {
      wx.showToast({
        title: '请选择是否在医院就诊'
      })
    } else if (value.desc == '') {
      wx.showToast({
        title: '请填写询问内容'
      })
    } else {
      wx.request({
        url: getApp().globalData.urls + '/wxgz/advisory/saveAdvisory',
        data: {
          token: wx.getStorageSync("token"),
          petType: value.type == "猫猫" ? 1 : 2,
          petGender:value.sex ==  "公" ? 1 : 2,
          petName: value.petName,
          petAge: value.petAge,
          sterilization: value.ster == "是" ? 1 : 2,
          duration: that.data.time,
          treated: value.treat == "是" ? 1 : 2,
          content: value.desc
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {

          if (res.data.code == 200) {
            var orderid = res.data.data.orderId
            console.log(orderid)
            wx.request({
              url: getApp().globalData.urls + '/wxgz/weixinPay/getWXAPPPayMap',
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                orderId: orderid,
                token: wx.getStorageSync("token")
              },
              success: function (res) {
                console.log(res.data)
                if (res.data.code == 200) {
                  console.log(typeof (res.data.data.timestamp), typeof (res.data.data.noncestr))
                  wx.requestPayment({

                    'timeStamp': res.data.data.timeStamp.toString(),
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': res.data.data.signType,
                    'paySign': res.data.data.paySign,
                    'success': function (res) {
                      let paytime = that.getTime();
                      console.log('111',res)
                      wx.navigateBack({
                        delta:1,
                      })
                    
                      wx.request({
                        url: 'https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=' + code + '&grant_type=authorization_code',
                        data: {},
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: function (res) {
                          
                          let openid = res.data.openid //返回openid
                          console.log(openid)
                          wx.request({
                            url: getApp().globalData.urls + '/wxgz/accessToken/getAccessToken',
                            method: "POST",
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            success:function(res){
                              console.log(res,paytime)
                              let accestoken = res.data.data
                              wx.request({
                                url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='+accestoken,
                                method: 'POST',
                                
                                header: {
                                  'content-type': 'application/x-www-form-urlencoded'
                                },
                                data: {
                                  "touser": openid,
                                  "template_id": "_5VfDJ_Um4bWJdWjicBJm2Tkl6ghRk3ygkLh4YIjk_Q",
                                  "form_id": formId,
                                  data: {
                                    "keyword1": {
                                      "value": paytime
                                    },
                                    "keyword2": {
                                      "value": "9.9"
                                    },
                                    "keyword3": {
                                      "value": "乖爪咨询"
                                    },
                                    "keyword4": {
                                      "value": "您已付款成功，请等待回复"
                                    }
                                  }
                                },
                                success: function (res) {
                                  console.log(res, '111')
                                }, fail: function (res) {
                                  console.log(res, '222')
                                }
                              })
                            }
                          })
                          
                        }
                      })

                      
                      
                      
                    }, 'fail': function (res) {
                      
                    }
                  })

                }
              }
            })
          }
        }
      })
    }
  


  },
  getTime: function () {
    var date = new Date();
    var year = date.getFullYear();
    var mouth = date.getMonth() + 1
    var day = date.getDate();
    var hours = date.getHours();
    var minute = date.getMinutes();
    return year + '-' + that.add0(mouth) + '-' + that.add0(day) + '  ' + that.add0(hours) + ':' + that.add0(minute)
  },
  add0: function (num) {
    return num < 10 ? '0' + num : '' + num;
  }
  // /**
  //  * 生命周期函数--监听页面初次渲染完成
  //  */
  // onReady: function () {
  
  // },

  // /**
  //  * 生命周期函数--监听页面显示
  //  */
  // onShow: function () {
  
  // },

  // /**
  //  * 生命周期函数--监听页面隐藏
  //  */
  // onHide: function () {
  
  // },

  // /**
  //  * 生命周期函数--监听页面卸载
  //  */
  // onUnload: function () {
  
  // },

  // /**
  //  * 页面相关事件处理函数--监听用户下拉动作
  //  */
  // onPullDownRefresh: function () {
  
  // },

  // /**
  //  * 页面上拉触底事件的处理函数
  //  */
  // onReachBottom: function () {
  
  // },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {
  
  // }
})